<template>
  <view>
    <!-- 搜索组件 -->
    <view class="search-box">
      <my-search @click="goToSearch" />
    </view>
    <!-- 轮播图 -->
    <view>
      <swiper indicator-dots autoplay circular>
        <navigator
          :url="'/subpkg/goods_detail/index?goods_id=' + item.goods_id"
          open-type="navigate"
          hover-class="navigator-hover"
          v-for="item in swiperList"
          :key="item.goods_id"
        >
          <swiper-item>
            <image :src="item.image_src" mode="scaleToFill" />
          </swiper-item>
        </navigator>
      </swiper>
    </view>
    <!-- 菜单列表 -->
    <view class="nav-list">
      <image
        v-for="item in navList"
        :key="item.name"
        :src="item.image_src"
        class="nav-img"
        mode="scaleToFill"
        @click="navClick(item)"
      />
    </view>
    <!-- 楼层 -->
    <view>
      <view v-for="item in floorList" :key="item.floor_title.image_src">
        <!-- 标题区域 -->
        <image
          class="floor-title"
          :src="item.floor_title.image_src"
          mode="widthFix"
        />
        <!-- 楼层图片区域 -->
        <view class="floor-img-box">
          <!-- 左边图片 -->
          <navigator
            :url="item.product_list[0].navigator_url"
            open-type="navigate"
            hover-class="navigator-hover"
          >
            <!-- 左侧大图片的盒子 -->
            <image
              :style="{
                width: item.product_list[0].image_width + 'rpx'
              }"
              mode="widthFix"
              :src="item.product_list[0].image_src"
            />
          </navigator>
          <!-- 右边图片 -->
          <view class="right-img-box">
            <!-- 右侧 4 个小图片的盒子 -->
            <navigator
              :url="subitem.navigator_url"
              open-type="navigate"
              hover-class="navigator-hover"
              v-for="(subitem, index) in item.product_list"
              v-show="index > 0"
              :key="subitem.name"
            >
              <image
                :src="subitem.image_src"
                mode="widthFix"
                :style="{ width: subitem.image_width + 'rpx' }"
              />
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { request } from '@/utils/request.js'
import MySearch from '@/components/my-search'
// 导入自己封装的 mixin 模块
import badgeMix from '@/mixins/tabbar-badge.js'
export default {
  // 将 badgeMix 混入到当前的页面中进行使用
  mixins: [badgeMix],
  components: {
    MySearch
  },
  data () {
    return {
      swiperList: [], // 轮播图列表
      navList: [], // 菜单列表
      floorList: [] //  楼层的数据列表
    }
  },
  onLoad () {
    this.getSwiperList()
    this.getNavList()
    this.getFloorList()
  },
  methods: {
    async getSwiperList () {
      const res = await request({
        url: '/api/public/v1/home/swiperdata'
      })

      if (res.meta.status === 200) {
        this.swiperList = res.message
      }
    },
    async getNavList () {
      const res = await await request({
        url: '/api/public/v1/home/catitems'
      })

      if (res.meta.status === 200) {
        this.navList = res.message
      }
    },
    async getFloorList () {
      const res = await await request({
        url: '/api/public/v1/home/floordata'
      })

      if (res.meta.status === 200) {
        res.message.forEach(item => {
          item.product_list.forEach(subitem => {
            subitem.navigator_url =
              '/subpkg/goods_list/index?' + subitem.navigator_url.split('?')[1]
          })
        })
        this.floorList = res.message
      }
    },
    // 跳转到搜索页面
    goToSearch () {
      uni.navigateTo({
        url: '/subpkg/search/index'
      })
    },
    // 菜单点击了
    navClick ({ name }) {
      if (name === '分类') {
        uni.switchTab({ url: '/pages/category/index' })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.search-box {
  // 设置定位效果为“吸顶”
  position: sticky;
  // 吸顶的“位置”
  top: 0;
  // 提高层级，防止被轮播图覆盖
  z-index: 999;
}

swiper {
  height: 400rpx;

  .swiper-item,
  image {
    width: 100%;
    height: 100%;
  }
}

.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 15px 0;

  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}

.floor-title {
  height: 60rpx;
  width: 100%;
  display: flex;
}

.right-img-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-img-box {
  display: flex;
  padding-left: 10rpx;
}
</style>
